﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BasicClass;
using System.Collections;
using System.Windows.Media;
using Common;
using System.IO;
using DealFile;
using System.Diagnostics;

namespace DealPLC
{

    public class ParLogReadWritePLC : BaseClass
    {
        #region 静态类示例
        public static ParLogReadWritePLC P_I = new ParLogReadWritePLC();
        #endregion 静态类示例

        #region 定义
        #region Path
        public string Path
        {
            get
            {
                string path = ComValue.c_PathRecord + "PLC通信日志\\";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string strPath = Log.CreateAllTimeFile(path)+"PLC读写综合记录.Log";
                return strPath;
            }
        }
        #endregion Path
        public int g_Num = 0;
        double TimefoTxt = 0;//记录txt耗时

        public Hashtable Ht_RegPLC = new Hashtable();//记录了所有PLC寄存器,后续用来比较信息

        string TimeNow
        {
            get
            {
                return DateTime.Now.Hour.ToString().PadLeft(2, ' ') + ":" + DateTime.Now.Minute.ToString().PadLeft(2, ' ') + ":" + DateTime.Now.Second.ToString().PadLeft(2, ' ') + ":" + DateTime.Now.Millisecond.ToString().PadLeft(3, ' ');//时间
            }
        }
        public string Time { get; set; }

        public string Reg { get; set; }//PLC寄存器名称
        public string Data { get; set; }//数据
        public string Co { get; set; }//系数
        public string Type { get; set; }//读写类型

        public List<ParLogReadWritePLC> ParLogReadWritePLC_L = new List<ParLogReadWritePLC>();

        /// <summary>
        /// 列表row颜色
        /// </summary>
        public SolidColorBrush BrReg
        {
            get
            {
                SolidColorBrush colorSpeet = Brushes.Black;
                if (Type == "读")
                {
                    colorSpeet = Brushes.Blue;
                }
                return colorSpeet;
            }
        }

        //索引器
        public ParLogReadWritePLC this[int index]
        {
            get
            {
                if (ParLogReadWritePLC_L.Count - 1 < index)
                {
                    return null;
                }
                return ParLogReadWritePLC_L[index];
            }
        }


        #endregion 定义

        #region 初始化
        public ParLogReadWritePLC()
        {
            NameClass = "ParLogReadWritePLC";
            
        }


        #endregion 初始化


        /// <summary>
        /// 添加读取的信息
        /// </summary>
        /// <param name="reg"></param>
        /// <param name="value"></param>
        public void AddInfo_Read(string reg, double value)
        {
            try
            {
                reg = reg.Replace("\\n", "\n");

                g_Num++;

                ParLogReadWritePLC infoPar = new ParLogReadWritePLC();
                infoPar.No = g_Num;
                infoPar.Reg = reg;
                infoPar.Data = value.ToString();
                infoPar.Co = 1.ToString();
                infoPar.Time = TimeNow;
                infoPar.Type = "读";
                infoPar.Annotation = "设置中无此寄存器";
                //获取注释
                foreach (string item in Ht_RegPLC.Keys)
                {
                    if (item.Contains(reg))
                    {
                        RegPLC inst = (RegPLC)Ht_RegPLC[item];
                        infoPar.Annotation = inst.Annotation;
                    }
                }

                ParLogReadWritePLC_L.Add(infoPar);

                //记录到本地
                WriteTxt(infoPar);
                if (ParLogReadWritePLC_L.Count > 150)
                {
                    ParLogReadWritePLC_L.RemoveRange(0, ParLogReadWritePLC_L.Count - 150);
                }
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 添加写入的信息
        /// </summary>
        /// <param name="reg"></param>
        /// <param name="value"></param>
        public void AddInfo_Write(string reg, double value)
        {
            try
            {
                reg = reg.Replace("\\n", "\n");

                g_Num++;

                ParLogReadWritePLC infoPar = new ParLogReadWritePLC();
                infoPar.No = g_Num;
                infoPar.Reg = reg;
                infoPar.Data = value.ToString();
                infoPar.Co = "";
                infoPar.Time = TimeNow;
                infoPar.Type = "写";
                infoPar.Annotation = "设置中无此寄存器";
                //获取注释
                foreach (string item in Ht_RegPLC.Keys)
                {
                    if (item.Contains(reg))
                    {
                        RegPLC inst = (RegPLC)Ht_RegPLC[item];
                        infoPar.Annotation = inst.Annotation;
                        infoPar.Co = inst.Co.ToString();
                    }
                }

                ParLogReadWritePLC_L.Add(infoPar);
                //记录到本地
                WriteTxt(infoPar);

                if (ParLogReadWritePLC_L.Count > 150)
                {
                    ParLogReadWritePLC_L.RemoveRange(0, ParLogReadWritePLC_L.Count - 150);
                }
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }


        /// <summary>
        /// 写入文本
        /// </summary>
        /// <param name="stateInfo"></param>
        public void WriteTxt(ParLogReadWritePLC info)
        {
            try
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                TxtFile t_I = new TxtFile();
                string txt = string.Format("[{5}]序号:{0}\n寄存器:{1},数值:{2}，系数:{7},类型:{3}，  注释:{4},  Txt记录耗时:{6}\n",
                    info.No.ToString().PadRight(5, ' '), info.Reg.Replace("\n", "\\n").PadRight(20, ' '), info.Data.ToString().PadRight(20, ' '),
                    info.Type, info.Annotation, info.Time, TimefoTxt.ToString(), info.Co.ToString().PadRight(7, ' '));

                t_I.WriteText(Path, txt);

                sw.Stop();
                TimefoTxt = sw.ElapsedMilliseconds;
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
            }
        }
    }
}
